package com.cqjtu.eecs.utils.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.CourseType;
import com.cqjtu.eecs.entity.TrainingProjectCourse;
import com.cqjtu.eecs.entity.dto.TrainingProjectCourseDto;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.service.ICourseService;
import com.cqjtu.eecs.service.ICourseTypeService;
import com.cqjtu.eecs.service.ITrainingProjectCourseService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 培养目标课程excel监听器
 * @author: 徐文升
 * @create: 2021-12-12 16:12
 **/

public class TrainingProjectCourseListener extends AnalysisEventListener<TrainingProjectCourseDto> {
    /**
     * 专业代码
     */
    private String majorCode;

    /**
     * 版本编号
     */
    private String versionCode;

    private Map<String, Integer> courseTypeMap;

    private Map<String, Integer> courseNameMap;

    private ICourseTypeService courseTypeService;

    private ICourseService courseService;

    private List<TrainingProjectCourse> trainingProjectCourseList = new ArrayList<>();

    private ITrainingProjectCourseService trainingProjectCourseService;

    public TrainingProjectCourseListener(String majorCode, String versionCode, ICourseTypeService courseTypeService,
                                         ITrainingProjectCourseService projectCourseService, ICourseService courseService) {
        this.majorCode = majorCode;
        this.versionCode = versionCode;
        this.courseTypeService = courseTypeService;
        this.courseService = courseService;
        this.trainingProjectCourseService = projectCourseService;
        //获取所有课程类别的名称-编号键值对
        courseTypeMap = courseTypeService.list()
                .stream()
                .collect(Collectors.toMap(CourseType::getCourseTypeName, CourseType::getCourseTypeId));
        //获取所有课程的名称-编号键值对
        courseNameMap = courseService.list().stream().collect(Collectors.toMap(Course::getCourseName, Course::getCourseId));
    }

    @Override
    public void invoke(TrainingProjectCourseDto data, AnalysisContext analysisContext) {
        BigDecimal credit = null;
        if (data.getCredit() != null) {
            credit = new BigDecimal(data.getCredit());
        }

        if (data.getCourseName() != null) {
            String courseName = data.getCourseName();
            data.setCourseName(courseName.replaceAll(" ", ""));
        }

        if (courseNameMap.get(data.getCourseName()) == null)
            throw new NotFoundException("课程" + data.getCourseName() + "不存在，请核对信息后重新上传");

        if (data.getCourseCode() != null)
            trainingProjectCourseList.add(new TrainingProjectCourse(
                    data.getCourseCode(), courseNameMap.get(data.getCourseName()), this.majorCode, this.versionCode,
                    courseTypeMap.get(data.getCourseType()), data.getTerm(),
                    credit, data.getEvaluationMode(),
                    data.getCourseProperty(), data.getPeriod(), data.getTheoreticalPeriod(),
                    data.getPracticalPeriod(), data.getComputerPeriod(), data.getExperimentPeriod()
            ));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        trainingProjectCourseService.saveBatch(trainingProjectCourseList);
        trainingProjectCourseList.clear();
    }
}
